package com.dragon.graduation.student.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.graduation.student.controller.dto.StudentAddDTO;
import com.dragon.graduation.student.controller.dto.StudentSearchDTO;
import com.dragon.graduation.student.controller.dto.StudentUpdateDTO;
import com.dragon.graduation.student.controller.vo.StudentVO;
import com.dragon.graduation.student.entity.StudentEntity;
import com.dragon.graduation.student.service.StudentService;
import com.dragon.graduation.user.service.UserService;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:22:17
 */
@RestController
@RequestMapping("/student")
@RequiredArgsConstructor
@Api(tags = "学生表 相关接口")
public class StudentController {

    private final StudentService studentService;
    private final UserService userService;


    @PostMapping("add")
    @ApiOperation("1]学生表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody StudentAddDTO addDTO) {
        StudentEntity entity = StudentAddDTO.to(addDTO, StudentEntity.class);
        Integer userId = userService.addUser(entity.getStudentNumber(), addDTO.getStudentName(), 2);
        entity.setUserId(userId);
        studentService.save(entity);
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]学生表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody StudentUpdateDTO updateDTO) {
        StudentEntity entity = studentService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = StudentUpdateDTO.to(updateDTO, StudentEntity.class);
        studentService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]学生表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<StudentVO>>> search(@RequestBody StudentSearchDTO searchDTO) {
        QueryWrapper<StudentEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, StudentEntity.class);

        Page<StudentEntity> entityPage = MyPage.setPage(StudentEntity.class, searchDTO.getPage());
        Page<StudentEntity> page = studentService.page(entityPage, queryWrapper);
        ResultPage<List<StudentVO>> resultPage = MyPage.copyPage(page, StudentVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]学生表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        studentService.removeByIds(idList);
        userService.removeByUserIds(idList);
        return HttpResult.ok("删除成功");
    }
}
